﻿namespace Nello.Web
{
    using Nello;
    using Nello.Web.Controls;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class PageUtil
    {
        private static void AddControls(Control container, List<Control> list)
        {
            int count = container.Controls.Count;
            for (int i = 0; i < count; i++)
            {
                list.Add(container.Controls[i]);
                AddControls(container.Controls[i], list);
            }
        }

        public static void Assign(Control container, string key, string value)
        {
            List<Control> list = GetControls(container);
            foreach (Control ctl in list)
            {
                if (ctl is LiteralControl)
                {
                    (ctl as LiteralControl).Text = (ctl as LiteralControl).Text.Replace("${" + key + "}", value);
                }
            }
        }

        public static void ClearAll(Control container)
        {
            List<Control> list = GetControls(container);
            foreach (Control ctl in list)
            {
                if (ctl is TextBox)
                {
                    (ctl as TextBox).Text = "";
                }
            }
        }

        public static bool DoValidate(Page page)
        {
            List<Control> list = GetControls(page);
            foreach (Control ctl in list)
            {
                if (ctl is NTextBox)
                {
                    NTextBox dc = (NTextBox) ctl;
                    if (dc.IsRequired && (dc.DataValue.ToString() == ""))
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        public static void ExchangeData(Hashtable ht, Page page)
        {
            List<Control> list = GetControls(page);
            foreach (Control ctl in list)
            {
                if (ctl is IDataControl)
                {
                    IDataControl ictl = ctl as IDataControl;
                    if (!ictl.IsIgnore)
                    {
                        ht.Add(ctl.ClientID, ictl.DataValue);
                    }
                }
            }
        }

        public static void ExchangeData(Page page, Hashtable ht)
        {
            ExchangeData(page, ht, true);
        }

        public static void ExchangeData(Page page, DataTable dt)
        {
            ExchangeData(page, dt, true);
        }

        public static void ExchangeData(Page page, Hashtable ht, bool isOverwrite)
        {
            List<Control> list = GetControls(page);
            foreach (Control ctl in list)
            {
                if (ctl is IDataControl)
                {
                    IDataControl dc = ctl as IDataControl;
                    if (!(!(dc.DataValue.ToString() != "") || isOverwrite))
                    {
                        continue;
                    }
                    dc.DataValue = ht[ctl.ClientID];
                }
            }
        }

        public static void ExchangeData(Page page, DataTable dt, bool isOverwrite)
        {
            Hashtable ht = new Hashtable();
            DataUtil.ExchangeData(ht, dt.Rows[0], ExchangeType.AllColumn);
            ExchangeData(page, ht, isOverwrite);
        }

        public static List<string> GetControlNames(List<Control> controls)
        {
            List<string> list = new List<string>();
            foreach (Control ctl in controls)
            {
                list.Add(ctl.ClientID);
            }
            return list;
        }

        public static List<Control> GetControls(Control container)
        {
            List<Control> list = new List<Control>();
            AddControls(container, list);
            return list;
        }

        public static void SetEnabled(Control container, bool enabled)
        {
            SetEnabled(container, typeof(WebControl), enabled);
        }

        public static void SetEnabled(Control container, Type type, bool enabled)
        {
            List<Control> list = GetControls(container);
            if (type == typeof(WebControl))
            {
                foreach (Control ctl in list)
                {
                    if (ctl is WebControl)
                    {
                        (ctl as WebControl).Enabled = enabled;
                    }
                }
            }
            else
            {
                foreach (Control ctl in list)
                {
                    if (ctl.GetType() == type)
                    {
                        (ctl as WebControl).Enabled = enabled;
                    }
                }
            }
        }

        public static void SetReadOnly(Control container, bool breadonly)
        {
            List<Control> list = GetControls(container);
            foreach (Control ctl in list)
            {
                if (ctl is TextBox)
                {
                    (ctl as TextBox).ReadOnly = breadonly;
                }
            }
        }

        public static void SetVisible(Control container, bool bVisible)
        {
            SetVisible(container, typeof(Control), bVisible);
        }

        public static void SetVisible(Control container, Type type, bool bVisible)
        {
            List<Control> list = GetControls(container);
            if (type == typeof(Control))
            {
                foreach (Control ctl in list)
                {
                    ctl.Visible = bVisible;
                }
            }
            else if (type == typeof(WebControl))
            {
                foreach (Control ctl in list)
                {
                    if (ctl is WebControl)
                    {
                        ctl.Visible = bVisible;
                    }
                }
            }
            else
            {
                foreach (Control ctl in list)
                {
                    if (ctl.GetType() == type)
                    {
                        ctl.Visible = bVisible;
                    }
                }
            }
        }
    }
}

